package com.hd.manager.shiro.token;



import com.hd.appcommon.entity.Privilege;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

public class PrivilegeCache {

    private static final ConcurrentMap<String, Privilege> map = new ConcurrentHashMap<>();

    private static volatile boolean init = false;

    public static void init(List<Privilege> privileges) {
        privileges.forEach(p -> map.put(p.getPrivilegeId(), p));
        init = true;
    }

    public static Privilege put(String privilegeId, Privilege privilege) {
        return map.put(privilegeId, privilege);
    }

    public static Privilege replace(String privilegeId, Privilege privilege) {
        return map.replace(privilegeId, privilege);
    }

    public static Privilege get(String privilegeId) {
        return map.get(privilegeId);
    }

    public static Collection<Privilege> getAll() {
        return map.values();
    }

    public static List<Privilege> getByList(List<String> privilegeIds) {
        return privilegeIds.stream()
                .map(id -> map.get(id))
                .filter(p -> p != null)
                .collect(Collectors.toList());
    }
}